<?php
/**
 * Created by coder meng.
 * User: coder meng
 * Date: 2018/4/9 13:46
 * 单品
 */
namespace app\home\controller;
use cmf\controller\UserBaseController;
use app\home\model\MenuModel as ME;
use think\Db;
class MenuController extends UserBaseController
{
    /*
     * 操作员信息
     * */
    public function session()
    {
       $session=cmf_get_current_user();      
       return $session;
    }
    /*
     * 左边框内容
     * 2018-05-31 商品属性
     * */
    public function right()
    {
        $this->checkUserLogin();
        $session=$this->session();
        $shop_id=$session['store_id'];
        $meun=new ME();
        $typology=$meun->index();//总类型 不受选择的影响
        $goods=$meun->goods($shop_id);
        /* 属性*/
        $status=DB::name('goods_state')->where(['status'=>1])->select();
        $this->assign('status', $status);
        $this->assign('setmeal',0);
        $this->assign('data',$goods);
        $this->assign('category',$typology);
        return $this->fetch('shop/index');
    }
    /*
     * 单品菜单选择
     * 完善商品的价格有规格表来确认
     * */
    public function menu()
    {
        $session=$this->session();
        $store=DB::name('store')->where(['id'=>$session['store_id']])->find();
        $session['store_name']=$store['store_name'];
        $this->assign('session',$session);
        return $this->fetch('shop/home');
    }
    /*
     * 单种类别查询
     * 套餐
     * */
    public function show()
    {
        $menu=new ME();
        $id=$_POST['id'];
        $session=$this->session();
        $shop_id=$session['store_id'];
        if($id=='taocan')
        {
            $cause['set_shopid']=$shop_id;
            $data=$menu->set($cause);
        }else{
            $data=$menu->goods($shop_id,$id);
        }
        $this->success($data);
    }
    /*
     * 添加购物
     * 添加商品数量和小计
     * 2018/7-31 王磊  同一个订单的同一款产品同一种商品状态订单详情表中存储到一起 在数据库中添加相同小计和数量
     * 同一个订单 同一款产品的不同状态下 分开存储
     * */
    public function cart()
    {
        /*
         * 存储数据库方式
         * */
           $menu=new  ME();
           $array=$this->request->post();

           $session=$this->session();
           if($array['setmeal']==0) {

               $good = $menu->good($array['id'],$session['store_id']);
           }else{
               $where['set_id']=$array['id'];
               $where['set_shopid']=$session['store_id'];
               $good =DB::name('setmeal')->where($where)->find();
           }
           $good['setmeal']=$array['setmeal'];
           $order=$menu->saveorder($good,$session,$array);
           $date=$menu->content($order['orderNo']);
           return $this->success($date);
    }
    /*
     * 加减商品
     * 存储数据库方式
     * 2018-08-01  阿拉蕾  同一种订单的 同种商品下的同种属性相加
     * */
    public function addminus()
    {
        $menu=new  ME();
        $session=$this->session();
        $array=$this->request->post();
        $where['id']=$array['detail_id'];
        $where['orderid']=$array['orderid'];
        $cause['orderNo']=$array['orderid'];
        $cause['storeid']=$session['store_id'];
        $detail=DB::name('order_detail')->where($where)->find();
        $old_order=DB::name('order')->where($cause)->find();
        if($array['stype']==1)
        {
           $data['total']=$detail['total']+$detail['money'];
           $data['count']=$detail['count']+1;
           $new_detail=DB::name('order_detail')->where($where)->update($data);
           if($new_detail)
           {
               $order_date['totalMoney']=$old_order['totalMoney']+$detail['price'];
               $order_date['actualMoney']=$old_order['actualMoney']+$detail['money'];
               $order=DB::name('order')->where($cause)->update($order_date);
           }else{
               $order=0;
           }
        }else{
            $data['total']=$detail['total']-$detail['money'];
            if($data['total']<0)
            {
                $data['total']=0;
            }
            $data['count']=$detail['count']-1;
            $del['orderid']=$array['orderid'];
            $del['storeid']=$session['store_id'];
            $sel_order=DB::name('order_detail')->where($del)->find();
            if($data['count']==0)
            {
                $order_detail=DB::name('order_detail')->where($where)->delete();
            }else{
                $new_detail=DB::name('order_detail')->where($where)->update($data);
            }
            $sel_order=DB::name('order_detail')->where($del)->find();
            if($sel_order)
            {
                $order_date['totalMoney']=$old_order['totalMoney']-$detail['price'];
                $order_date['actualMoney']=$old_order['actualMoney']-$detail['money'];
                if($order_date['actualMoney']<0)
                {
                    $order_date['actualMoney']=0;
                }
                $order=DB::name('order')->where($cause)->update($order_date);
            }else{
                $order=DB::name('order')->where($cause)->delete();
            }
        }
        if($order == 1)
        {
            $date=$menu->content($array['orderid']);
        }else{
            $date['date']=0;
            $date['pay']=0;
        }
        return $this->success($date);
    }
    /*
     * 获取商品的属性
     * 在判断数据库是否相同的数据，有相同数据时候进行合并删除 并留下第一个订单详情id
     * */
    public function goods_state()
    {
         $me=new ME();
        $orderid=$this->request->param();
        if(!empty($orderid['orderid']))
        {
            $where['orderid']=$_POST['orderid'];
            $where['goodsid']=$_POST['goodsid'];
            $where['nature']=$_POST['id'];
          //$where['id']=$_POST['detail_id'];
            $old=DB::name('order_detail')->where($where)->find();
            if($old)
            {
                $cause['orderid']=$_POST['orderid'];
                $cause['id']=$_POST['detail_id'];
                $cause['goodsid']=$_POST['goodsid'];
                $old_detail=DB::name('order_detail')->where($cause)->find();
                $new_array['total']=$old['total']+$old_detail['total'];
                $new_array['count']=$old['count']+$old_detail['count'];
                $upd=DB::name('order_detail')->where($where)->update($new_array);
                if($upd)
                {
                    $del=DB::name('order_detail')->where($cause)->delete();
                    if($del)
                    {
                        $data=$me->content($_POST['orderid']);
                    }else{
                        $date['data']=0;
                        $date['pay']=0;
                    }
                }else{
                    $date['data']=0;
                    $date['pay']=0;
                }
            }else{
                $why['orderid']=$_POST['orderid'];
                $why['goodsid']=$_POST['goodsid'];
                $why['id']=$_POST['detail_id'];
                $data['nature']=$_POST['id'];
                $state=DB::name('order_detail')->where($why)->update($data);
                if($state)
                {
                    $data=$me->content($_POST['orderid']);
                }else{
                    $date['data']=0;
                    $date['pay']=0;
                }
            }
            $this->success($data);
        }else{
            $data=$me->goods_state();
            $array=$this->request->param();
            $this->assign('data',$array);
            $this->assign('status',$data);
            return $this->fetch('shop/goods_state');
        }
    }
    /**
     * 商品改价
     * 2018-08-03 name 阿拉蕾
    */
    public function updategoods(){
        $menu=new ME();
        $session=$this->session();
        $array=$this->request->param();
        $where['orderid']=$array['orderid'];
        $where['id']=$array['detail_id'];
        $date['total']=$array['gaijia'];
        $old_detail=DB::name('order_detail')->where($where)->find();
        $state=DB::name('order_detail')->where($where)->update($date);
        if($state)
        {
           $cause['orderNo']=$array['orderid'];
           $cause['storeid']=$session['store_id'];
           $old_order=Db::name('order')->where($cause)->find();
           $new_date['actualMoney']=$old_order['actualMoney']-$old_detail['total']+$array['gaijia'];
            if($new_date['actualMoney']<0)
            {
                $new_date['actualMoney']=0;
            }
           $new_order=DB::name('order')->where($cause)->update($new_date);
           if($new_order)
           {
               $data['state']=1;
               $data['actualMoney']=$new_date['actualMoney'];
           }else{
               $data['state']=0;
           }
        }else{
            $data['state']=0;
        }
        $this->success($data);
    }
    /*
     * 清空购物车
     * */
  public  function  emptys()
    {
        $orderid=$_POST['orderid'];
        $order= DB::name('order')->where(['orderNo'=>$orderid])->find();
        if($order['payStatus'] == -1)
        {
            $state=0;
            return $this->success($state);
        }else{
            DB::name('order_detail')->where(['orderid'=>$orderid])->delete();
            DB::name('order')->where(['orderNo'=>$orderid])->delete();
            $state=1;
            return $this->success($state);
        }
    }
    /*
     * 挂单
     * */
    public function updata()
    {
        $menu = new ME();
        $orderid=$this->request->param();
        if(!empty($orderid['orderid']))
        {
            $date['payStatus']=-1;
            $where['orderNo']=$orderid['orderid'];
            $data=$menu->updata($date,$where);
            $array=1;
            $this->success($array);
        }else{
            $where['payStatus']=-1;
            $data=DB::name('order')->where($where)->select();
            $this->assign('update',$data);
            return $this->fetch('shop/update');
        }
    }
    /*
     * 挂单详情
     * */
    public function  xq()
    {
        $orderid=$_POST['orderid'];
        $where['orderid']=$orderid;
        $order=DB::name('order_detail')->where($where)->select()->toArray();
        foreach($order as $k=>$v)
        {
            $format=$v['format'];
            $cause['id']=$format;
            $formats=DB::name('format')->where($cause)->find();
            $order[$k]['formatname']=$formats['format'];
        }
        return $this->success($order);
   }
    /*
     * 挂单选择
     * 挂单支付（未完成）
     * */
    public function xg()
    {
        $menu=new ME();
        $orderid=$_POST['orderid'];
        $date=$menu->content($orderid);
        $this->success($date);
    }
    /*全新改价*/
    public function  detail()
    {
        $array  =$this->request->param();
        $where['orderid']=$array['order'];
        $where['id']=$array['detail_id'];
        $detail=DB::name('order_detail')->where($where)->find();
        $this->assign('date',$detail);
       return $this->fetch('shop/edital');
    }
    /*改价接收数据*/
    public function detailpost(){
        $menu=new ME();
        $session=$this->session();
        $array=$this->request->param();
        $where['orderid']=$array['orderid'];
        $where['id']=$array['detail_id'];
        $data['total']=$array['gaijia'];
        $old_detail=DB::name('order_detail')->where($where)->find();
        $state=DB::name('order_detail')->where($where)->update($data);
        if($state)
        {
            $cause['orderNo']=$array['orderid'];
            $cause['storeid']=$session['store_id'];
            $old_order=Db::name('order')->where($cause)->find();
            $new_date['actualMoney']=$old_order['actualMoney']-$old_detail['total']+$array['gaijia'];
            $new_order=DB::name('order')->where($cause)->update($new_date);
            if($new_order)
            {
                $date=$menu->content($array['orderid']);
            }else{
                $date['data']=0;
                $date['pay']=0;
            }
        }else{
            $date['data']=0;
            $date['pay']=0;
        }
        $this->success($date);
    }
}